import { setStore, getStore } from '@/util/store';
import { diff } from '@/util/util';
import website from '@/config/website';
const isFirstPage = website.isFirstPage;
const tagWel = website.fistPage;
const tagObj = {
  label: '', //标题名称
  value: '', //标题的路径
  params: '', //标题的路径参数
  query: '', //标题的参数
  meta: {}, //额外参数
  icon: '',
  group: [] //分组
};

//处理首个标签
function setFistTag(list) {
  if (list.length === 1) {
    list[0].close = false;
  } else {
    list.forEach((ele) => {
      if (ele.value === tagWel.value && isFirstPage === false) {
        ele.close = false;
      } else {
        ele.close = true;
      }
    });
  }
}

const navs = {
  state: {
    cacheTagList: getStore({ name: 'cacheTagList' }) || [],
    tagList: getStore({ name: 'tagList' }) || [],
    tag: getStore({ name: 'tag' }) || tagObj,
    tagWel: tagWel
  },
  actions: {},
  mutations: {
    ADD_TAG: (state, action) => {
      action.value = (action.value || '').split('?')[0];
      state.tag = action;
      setStore({ name: 'tag', content: state.tag });
      let findTag = state.tagList.find((ele) => ele.value === action.value);
      if (findTag) return;
      state.tagList.push(action);
      state.cacheTagList.push(action);
      setFistTag(state.tagList);
      setStore({ name: 'tagList', content: state.tagList });
      setStore({ name: 'cacheTagList', content: state.cacheTagList });
    },
    DEL_TAG: (state, action) => {
      state.tagList = state.tagList.filter((item) => {
        return !diff(item, action);
      });
      // 查找cacheTagList中
      state.cacheTagList = state.cacheTagList.filter((item) => {
        return !diff(item, action);
      });
      state.cacheTagList.push(action);
      setStore({ name: 'cacheTagList', content: state.cacheTagList });
      setFistTag(state.tagList);
      setStore({ name: 'tagList', content: state.tagList });
    },
    DEL_ALL_TAG: (state) => {
      state.tagList = [state.tagWel];
      setStore({ name: 'tagList', content: state.tagList });
    },
    DEL_TAG_OTHER: (state) => {
      state.tagList = state.tagList.filter((item) => {
        if (item.value === state.tag.value) {
          return true;
        } else if (!website.isFirstPage && item.value === website.fistPage.value) {
          return true;
        }
      });
      setFistTag(state.tagList);
      setStore({ name: 'tagList', content: state.tagList });
    },
    DEL_LEFT_OTHER: (state) => {
      let currentTagIndex = state.tagList.findIndex((item) => item.value === state.tag.value);
      state.tagList.splice(1, currentTagIndex - 1);
      setFistTag(state.tagList);
      setStore({ name: 'tagList', content: state.tagList });
    },
    DEL_RIGHT_OTHER: (state) => {
      let currentTagIndex = state.tagList.findIndex((item) => item.value === state.tag.value);
      state.tagList = state.tagList.slice(0, currentTagIndex + 1);
      setFistTag(state.tagList);
      setStore({ name: 'tagList', content: state.tagList });
    },
    SET_TAG_LIST(state, tagList) {
      state.tagList = tagList;
      setStore({ name: 'tagList', content: state.tagList });
    },
    SET_CACHE_TAG_LIST(state, cacheTagList) {
      state.cacheTagList = cacheTagList;
      setStore({ name: 'cacheTagList', content: state.cacheTagList });
    }
  }
};
export default navs;
